package org.apache.poi.ext;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;

/**
 * @since poi 4.2
 */
public class ReadFile {

	/**
	 * 处理word2003 or  word2003
	 * 
	 * @param inputStream 文件流
	 * @return 文件内文本
	 * @throws Exception
	 */
	public static String readWord(InputStream inputStream) throws Exception {
		String bodyText = null;
		WordExtractor extractor = new WordExtractor(inputStream);
		bodyText = extractor.getText();
		extractor.close();
		return bodyText;
	}

	/**
	 * 处理word2007
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 * @throws OpenXML4JException
	 * @throws XmlException
	 */
	public static String readWord2007(String path) throws IOException, OpenXML4JException, XmlException {
		OPCPackage opcPackage = POIXMLDocument.openPackage(path);
		POIXMLTextExtractor ex = new XWPFWordExtractor(opcPackage);
		String ret = ex.getText();
		ex.close();
		return ret;
	}

	/**
	 * 处理excel2003
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String readExcel(String path) throws IOException {
		InputStream inputStream = null;
		String content = null;
		try {
			inputStream = new FileInputStream(path);
			HSSFWorkbook wb = new HSSFWorkbook(inputStream);
			ExcelExtractor extractor = new ExcelExtractor(wb);
			extractor.setFormulasNotResults(true);
			extractor.setIncludeSheetNames(false);
			content = extractor.getText();
			extractor.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return content;
	}

	/**
	 * 处理excel2007
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String readExcel2007(String path) throws IOException {
		StringBuffer content = new StringBuffer();
		// 构造 XSSFWorkbook 对象，strPath 传入文件路径
		XSSFWorkbook xwb = new XSSFWorkbook(path);
		// 循环工作表Sheet
		for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) {
			XSSFSheet xSheet = xwb.getSheetAt(numSheet);
			if (xSheet == null) {
				continue;
			}
			// 循环行Row
			for (int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++) {
				XSSFRow xRow = xSheet.getRow(rowNum);
				if (xRow == null) {
					continue;
				}
				// 循环列Cell
				for (int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++) {
					XSSFCell xCell = xRow.getCell(cellNum);
					if (xCell == null) {
						continue;
					}
					if (xCell.getCellType() == CellType.BOOLEAN) {
						content.append(xCell.getBooleanCellValue());
					} else if (xCell.getCellType() == CellType.NUMERIC) {
						content.append(xCell.getNumericCellValue());
					} else {
						content.append(xCell.getStringCellValue());
					}
				}
			}
		}
		xwb.close();
		return content.toString();
	}

	/**
	 * 处理txt
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String readTxt(String path) throws IOException {
		StringBuffer sb = new StringBuffer("");
		InputStream is = new FileInputStream(path);
		// 必须设置成GBK，否则将出现乱码
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GBK"));
		try {
			String line = "";
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\r");
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return sb.toString().trim();
	}
}
